package com.jy.modules.cims.component.image;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.component.image.task.AddWaitDealQueueTask;
import com.jy.modules.cims.component.image.task.DownImagesFromUrlTask;
import com.jy.modules.cims.component.image.task.QueryTransferImagesTask;
import com.jy.modules.cims.component.image.task.SendAttachTask;
import com.jy.modules.cims.component.image.task.SendBorrowerInfoTask;
import com.jy.modules.cims.component.image.task.UploadImagesToFTPTask;
import com.jy.modules.cims.component.image.task.ZipImagesByLocalTask;

/**
 * @description TransferImagesJob
 * @author shengzhoutao
 * @date 2015-09-29
 */
@Service("com.jy.modules.cims.component.image.TransferImagesJob")
@Lazy(false)
public class TransferImagesJob {
	private ExecutorService executorService = Executors.newCachedThreadPool();

	@Autowired
	private SpringContextUtils springContextUtils;

	@PostConstruct
	public void run() {
		AddWaitDealQueueTask addWaitDealQueueTask = springContextUtils.getBean(AddWaitDealQueueTask.class);
		executorService.execute(addWaitDealQueueTask);
		
		QueryTransferImagesTask queryTransferImagesTask = springContextUtils.getBean(QueryTransferImagesTask.class);
		executorService.execute(queryTransferImagesTask);

		DownImagesFromUrlTask downImagesFromUrlTask = springContextUtils.getBean(DownImagesFromUrlTask.class);	
		executorService.execute(downImagesFromUrlTask);
		executorService.execute(downImagesFromUrlTask);
		
		ZipImagesByLocalTask zipImagesByLocalTask = springContextUtils.getBean(ZipImagesByLocalTask.class);	
		executorService.execute(zipImagesByLocalTask);
		executorService.execute(zipImagesByLocalTask);

		UploadImagesToFTPTask uploadImagesToFTPTask = springContextUtils.getBean(UploadImagesToFTPTask.class);
		executorService.execute(uploadImagesToFTPTask);
		executorService.execute(uploadImagesToFTPTask);
		
		SendAttachTask sendAttachTask = springContextUtils.getBean(SendAttachTask.class);
		executorService.execute(sendAttachTask);
		executorService.execute(sendAttachTask);
		
		SendBorrowerInfoTask sendBorrowerInfoTask = springContextUtils.getBean(SendBorrowerInfoTask.class);
		executorService.execute(sendBorrowerInfoTask);
		executorService.execute(sendBorrowerInfoTask);
	}
	
	/**
	 * @description
	 */
	public void close() {// 当不再使用TransferImagesJob类的对象时，应该使用close方法关闭其中包含的ExecutorService接口的实现对象，否则虚拟机不会退出，占用内存不释放
		executorService.shutdown();// 发出关闭请求，此时不会再接受新任务
		try {
			if (!executorService.awaitTermination(3, TimeUnit.MINUTES)) {// awaitTermination
				// 来等待一段时间，使正在执行的任务或等待的任务有机会完成
				executorService.shutdownNow();// 如果等待时间过后还有任务没完成，则强制结束
				executorService.awaitTermination(1, TimeUnit.MINUTES);// 再等待一段时间，使被强制结束的任务完成必要的清理工作
			}
		} catch (InterruptedException e) {
			executorService.shutdownNow();
			Thread.currentThread().interrupt();
		}
	}
}
